package org.formal.common.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.formal.system.entity.FileEntity;
import org.formal.system.entity.UserEntity;
import org.formal.system.service.FileService;
import org.formal.system.shiro.manager.TokenManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

/**
 * 使用于页面跳转
 * @author jiangyan
 *
 */
@Controller
@RequestMapping(value = "/fashion")
public class ViewController {

	@Resource
	private FileService fileService;
	
	/**
	 * 默认访问路径
	 * @param name 文件名
	 * @param param 字符串参数集合
	 * @return
	 */
	@GetMapping(value = "/{name}")
	public ModelAndView getView(@PathVariable("name") String name, String param) {
		return getViewByPath(name, param); 
	}
	
	
	/**
	 * 一级访问路径
	 * @param name 文件名
	 * @param path 一级路径
	 * @param param 字符串参数集合
	 * @return
	 */
	@GetMapping(value = "/{path}/{name}")
	public ModelAndView getViewFirstLevel(@PathVariable("path") String path, @PathVariable("name") String name,
			String param) {
		return getViewByPath(path + "/" + name, param);
	}

	/**
	 * 二级访问路径
	 * @param name 文件名
	 * @param path 一级路径
	 * @param secondPath 二路路径
	 * @param param 字符串参数集合
	 * @return
	 */
	@GetMapping(value = "/{path}/{secondPath}/{name}")
	public ModelAndView getViewSecondLevel(@PathVariable("path") String path, @PathVariable("name") String name,
			String param, @PathVariable("secondPath") String secondPath) {
		return getViewByPath(path + "/" + secondPath + "/" + name, param);
	}

	/**
	 * 三级访问路径 
	 * @param path 一级路径
	 * @param name 文件名
	 * @param param 字符串参数集合
	 * @param secondPath 二路路径
	 * @param thirdPath 三级路径
	 * @return
	 */
	@GetMapping(value = "/{path}/{secondPath}/{thirdPath}/{name}")
	public ModelAndView getViewThirdLevel(@PathVariable("path") String path, @PathVariable("name") String name,
			String param, @PathVariable("secondPath") String secondPath, @PathVariable("thirdPath") String thirdPath) {
		return getViewByPath(path + "/" + secondPath + "/" + thirdPath + "/" + name, param);
	}

	@GetMapping(value = "/getView")
	public ModelAndView getViewWithPath(String path, String param) {
		ModelAndView view = new ModelAndView();
		if (StringUtils.isNoneBlank(param)) {
			view.addAllObjects(JSON.parseObject(param));
		}
		view.setViewName(path);
		return view;
	}
	
	/**
	 * 附件列表
	 * @return
	 */
	@GetMapping(value = "/file/list")
	public ModelAndView getFiles() {
		ModelAndView view = new ModelAndView();
		view.setViewName("view/system/file/file_management");
		List<FileEntity> files = fileService.selectAll();
		view.addObject("files", files);
		return view;
	}
	
	/**
	 * 菜单角色配置页面
	 * @return
	 */
	@GetMapping(value="/menu/role/config")
	public ModelAndView menuRoleConfig() {
		ModelAndView view = new ModelAndView();
		view.setViewName("view/system/menu/menu_role_config");
		return view;
	}
	
	/**
	 * 文件上传
	 */
	@GetMapping(value="/file/upload")
	public ModelAndView fileUpload(String url) {
		ModelAndView view = new ModelAndView();
		view.setViewName("view/system/file/file_upload");
		if(!url.startsWith("/")) {
			url = "/" + url;
		}
		view.addObject("url", url);
		return view;
	}
	
	/**
	 * 菜单保存页面
	 * @return
	 */
	@GetMapping(value="/menu/save")
	public ModelAndView menuSave() {
		ModelAndView view = new ModelAndView();
		view.setViewName("view/system/menu/menu_save");
		return view;
	}
	
	/**
	 * 角色保存页面
	 * @return
	 */
	@GetMapping(value="/role/save")
	public ModelAndView roleSave() {
		ModelAndView view = new ModelAndView();
		view.setViewName("view/system/role/role_save");
		return view;
	}
	
	/**
	 * 角色菜单配置
	 * @param id
	 * @return
	 */
	@GetMapping(value="/role/menu/config")
	public ModelAndView roleMenuConfig(String id) {
		ModelAndView view = new ModelAndView();
		view.setViewName("view/system/role/role_menu_config");
		view.addObject("id", id);
		return view;
	}
	
	/**
	 * 用户角色配置
	 * @return
	 */
	@GetMapping(value="/user/role/config")
	public ModelAndView userRoleConfig() {
		ModelAndView view = new ModelAndView();
		view.setViewName("view/system/user/user_role_config");
		return view;
	}
	
	/**
	 * 用户头像配置
	 * @return
	 */
	@GetMapping(value="/user/photo/config")
	public ModelAndView userPhotoConfig() {
		ModelAndView view = new ModelAndView();
		view.setViewName("view/system/user/picture_upload");
		return view;
	}
	
	/**
	 * 用户保存
	 * @return
	 */
	@GetMapping(value="/user/save")
	public ModelAndView userSave() {
		ModelAndView view = new ModelAndView();
		view.setViewName("view/system/user/user_save");
		return view;
	}
	
	/**
	 * 登录验证
	 * @param user
	 * @return
	 */
	@PostMapping(value = "/login")
	@ResponseBody
	public Map<String, Object> login(UserEntity user, HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String, Object>(2);
		if (StringUtils.isEmpty(user.getLoginName()) || StringUtils.isEmpty(user.getPassword())) {
			model.put("msg", "用户名或密码不能为空！");
			return model;
		}
		try {
			TokenManager.login(user, false);
			model.put("msg", "200");
			return model;
		} catch (LockedAccountException lae) {

			model.put("msg", "用户已经被锁定不能登录，请与管理员联系！");
			return model;
		} catch (AuthenticationException e) {
			model.put("msg", "用户或密码不正确！");
			return model;
		}
	}

	/**
	 * 退出清除所有信息
	 * @return
	 */
	@PostMapping("/logout")
	public String logout() {
		TokenManager.logout();
		return "redirect:/index";
	}

	/**
	 * 404页面
	 * @return
	 */
	@RequestMapping(value = "/error")
	public String getErrorPath() {
		return "/view/common/error";
	}

	public ModelAndView getViewByPath(String path, String param) {
		ModelAndView view = new ModelAndView();
		path = "view/" + path;
		view.setViewName(path);
		if (StringUtils.isNoneBlank(param)) {
			view.addAllObjects(JSON.parseObject(param));
		}
		return view;
	}
}
